package net.pws.oos.biz.bdmap;

import net.pws.common.persistence.EntityManager;
import net.pws.common.simplemq.Action;
import net.pws.common.simplemq.Context;
import net.pws.oos.biz.BizException;
import net.pws.oos.biz.model.Task;
import net.pws.oos.biz.service.MapService;

public class LocationAddressUpdater implements Action {
	
	private EntityManager entityManager;

	private MapService mapService;

	public LocationAddressUpdater(EntityManager entityManager, MapService mapService) {
		this.entityManager = entityManager;
		this.mapService = mapService;
	}

	public void execute(Context context) {
		Task task = (Task) context.getData();
		doExecute(task);
	}

	private void doExecute(Task task) {
		LocationData location = mapService.getAddress(task.getLongitude(), task.getLatitude());
		if (location == null) {
            throw new BizException("从百度地图获得位置信息失败！");
		}

		task.setLocationAddress(location.getLocationAddress());
		task.setLocationProvice(location.getLocationProvice());
		task.setLocationCity(location.getLocationCity());
		task.setLocationCounty(location.getLocationCounty());
		task.setLocationStreet(location.getLocationStreet());
		task.setLocationNumber(location.getLocationNumber());
		entityManager.persist(task);
	}
	
}
